home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8538 / 8538.xpi / content / boot / scripts / feedlyExBoot.js < prev   
Text File  |  2010-02-06  |  9KB  |  304 lines

  1. function $debug( msg  )
  2. {
  3.     var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
  4.                                 .getService(Components.interfaces.nsIConsoleService);
  5.             
  6.     consoleService.logStringMessage( "[feedly][extension]" + msg );
  7. }
  8.  
  9. var ExceptionUtils = function()
  10. {
  11.     var that = {};
  12.     
  13.     that.formatError = function( task, e )
  14.     {
  15.         var errorMsg = "[error]failed to " + task + " because " + e.name + " -- " + e.message;
  16.         
  17.         if( e.fileName != null )
  18.             errorMsg += " in file '" + e.fileName + "'"
  19.         
  20.         if( e.lineNumber != null )
  21.             errorMsg += " at line number " + e.lineNumber;
  22.         
  23.         return errorMsg;
  24.     }
  25.     return that;
  26. }();
  27.  
  28. var feedlyExBoot = function()
  29. {    
  30.     var that = {};
  31.  
  32.     //---------------------------------------------------------------------
  33.     // Browser progress listener
  34.     //---------------------------------------------------------------------
  35.     var NS_BINDING_ABORTED = 0x804b0002;
  36.     that.onStateChange         = function( wp, request, state, result ){}
  37.     that.onStatusChange     = function ( wp, request, result, messages ){}
  38.     that.onSecurityChange     = function ( wp, request, state ){}
  39.     that.onProgressChange     = function( ){ }
  40.  
  41.     var boot = null;
  42.     
  43.     var lastErrorMsg = null;
  44.     
  45.     // DELEGATE CALLS TO TO THE EXTENSION
  46.     that.onLocationChange    = function ( wp, request, locationURI )
  47.     {    
  48.         try 
  49.         {    
  50.             // I am not sure why, but this seems to happen sometimes.
  51.             if( locationURI == null )
  52.                 return;
  53.             
  54.             // no pre-processing for the error page.
  55.             if( locationURI.spec.indexOf( "chrome://feedly/content/boot/error.htm" ) == 0 )
  56.                 return;
  57.             
  58.             // If there is an error message, redirect the user to the UI displaying the error message.
  59.             if( lastErrorMsg != null && locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
  60.             {
  61.                 request.cancel( NS_BINDING_ABORTED );
  62.                 wp.DOMWindow.document.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( lastErrorMsg );
  63.                 return;    
  64.             }
  65.     
  66.             // no pre-processing for the splash page from this point on
  67.             if( locationURI.spec.indexOf( "chrome://feedly/content/boot/splash.htm" ) == 0 )
  68.                 return;
  69.             
  70.             if( boot != null && boot.getBootError() != null )
  71.             {
  72.                 if( locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
  73.                 {
  74.                     request.cancel( NS_BINDING_ABORTED );
  75.     
  76.                     // redirect browser
  77.                     var errorMsg = "extension loader can not look up boot component";
  78.                     if( boot != null )
  79.                         errorMsg = boot.getBootError();
  80.                         
  81.                     wp.DOMWindow.document.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( errorMsg );    
  82.                 }
  83.                 return;
  84.             }
  85.             
  86.             // feedly application not loaded yet. Display the splash screen.
  87.             if( that.extension == null )
  88.             {
  89.                 if( locationURI.spec.indexOf( "http://www.feedly.com/home" ) == 0 )
  90.                 {
  91.                     // DBH $debug( "redirecting from " + locationURI.spec + " to splash screen" );
  92.                     request.cancel( NS_BINDING_ABORTED );
  93.                     
  94.                     // redirect browser
  95.                     wp.DOMWindow.document.location = "chrome://feedly/content/boot/splash.htm?" + encodeURIComponent( "Updating feedly. Please wait..." );            
  96.                 }
  97.                 return;
  98.             }
  99.             else
  100.             {
  101.                 that.extension.onLocationChange( wp, request, locationURI );
  102.             }
  103.         }
  104.         catch( e )
  105.         {
  106.             $debug( "[boot][exception]" + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber );
  107.             var msg = devhd.utils.ExceptionUtils.formatError( "route request", e );
  108.             $debug( "[boot]" + msg );
  109.         }
  110.     }
  111.     
  112.     that.onImageClick = function()
  113.     {
  114.         if( that.extension != null )
  115.             that.extension.onImageClick();    
  116.     }
  117.  
  118.     that.onCommand = function( e )
  119.     {
  120.         if( that.extension == null )
  121.             loadPage( "chrome://feedly/content/boot/splash.htm?" + encodeURIComponent( "Updating feedly. Please wait..." ) );
  122.         else
  123.             that.extension.onCommand( e );    
  124.     }
  125.  
  126.     function loadPage( pageURI )
  127.     {
  128.         var tabBrowser = getBrowser();
  129.         
  130.         // navigate through list of existing tabs
  131.         var tabs = tabBrowser.tabContainer.childNodes;
  132.         for( var i = 0; i < tabs.length; i++ )
  133.         {
  134.             var aTab = tabs[ i ];
  135.             var aBrowser = tabBrowser.getBrowserForTab( aTab );
  136.             var currentLocationURI = new String( aBrowser.contentDocument.location );
  137.  
  138.             // per olivier's request, open feedly in existing empty page.            
  139.             if( currentLocationURI == "about:blank" )
  140.             {
  141.                 aBrowser.contentDocument.location.href = pageURI;
  142.                 tabBrowser.selectedTab = aTab;
  143.                 return;
  144.             }
  145.             
  146.             if( currentLocationURI.indexOf( "chrome://feedly/content" ) == 0 )
  147.             {
  148.                 if( aBrowser.contentDocument.location != pageURI )
  149.                     aBrowser.contentDocument.location.href = pageURI;
  150.  
  151.                 tabBrowser.selectedTab = aTab;
  152.                 return;
  153.             }
  154.         };
  155.         
  156.         // no pre-existing tabs. create a new one.
  157.           var tab = tabBrowser.addTab( pageURI ); 
  158.         var aBrowser = tabBrowser.getBrowserForTab( tab ); 
  159.            tabBrowser.selectedTab = tab;
  160.         return;
  161.     };
  162.  
  163.     // event notifying us that the boot has finished to load the core in memory
  164.     that.onCoreLoaded = function()
  165.     {
  166.         var phase = "";
  167.         try
  168.         {
  169.             // Load JS libraries
  170.             ///D $debug( "[extension loader] loading extension in memory..." );
  171.             
  172.             phase = "loading feedlyExtension.js in memory";
  173.             var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  174.                                      .getService( Components.interfaces.mozIJSSubScriptLoader );
  175.  
  176.             jsLoader.loadSubScript( "chrome://feedly/content/app/extension/feedlyExtension.js" );
  177.             
  178.             // ask extension to load itself.
  179.             ///D $debug( "[extension loader] asking extension to load..." );
  180.             phase = "asking extension to load";
  181.             feedlyExtension.onLoad();
  182.     
  183.             ///D $debug( "[extension loader] notifying the extension that the core is ready..." );
  184.             phase = "notifying the extension that the core is ready.";
  185.             feedlyExtension.onCoreLoaded( boot.lookupCore() );
  186.             
  187.             // assign the extension to the that only when everything is loaded and ready to make sure that
  188.             // the extension is protected and no location change can propage from this place.
  189.             that.extension = feedlyExtension;            
  190.         }
  191.         catch( e )
  192.         {
  193.             that.extension = null;
  194.             lastErrorMsg = " failed to load extension in phase: " + phase + " because: " + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber;
  195.             $debug( "[extension-boot]" + lastErrorMsg );
  196.         }
  197.     }
  198.     
  199.     that.onBootError = function( errorMsg )
  200.     {
  201.         $debug( "[extension loader] boot error: " + errorMsg );
  202.         lastErrorMsg = errorMsg;
  203.         displayError();
  204.     }
  205.     
  206.     function displayError()
  207.     {
  208.         var tabBrowser = getBrowser();
  209.         var tabs = tabBrowser.tabContainer.childNodes;
  210.         for( var i = 0; i < tabs.length; i++ )
  211.         {
  212.             var aTab = tabs[ i ];
  213.             var aBrowser = tabBrowser.getBrowserForTab( aTab );
  214.             var aLoc = new String( aBrowser.contentDocument.location );
  215.             if( aLoc.indexOf( "chrome://feedly/content" ) == 0 )
  216.             {
  217.                 aBrowser.contentDocument.location = "chrome://feedly/content/boot/error.htm?msg=" + encodeURIComponent( lastErrorMsg );
  218.             }
  219.         }
  220.     }
  221.     
  222.     function handleOnPageShow()
  223.     {
  224.         if( that.extension != null )
  225.             that.extension.onPageShown();    
  226.     }
  227.     
  228.     that.onLoad = function() 
  229.       {                    
  230.         try
  231.         {
  232.             // monitor the browser
  233.             getBrowser().addProgressListener( that );
  234.             getBrowser().addEventListener( "pageshow", handleOnPageShow, false );
  235.                                             
  236.             boot = Components.classes["@devhd.com/feedly-boot;1"]
  237.                         .getService( Components.interfaces.nsIFeedlyBoot )
  238.                         .wrappedJSObject;
  239.                         
  240.             if( boot == null )
  241.             {
  242.                 $debug( "[onload] skiping load sequence because could not link to core feedddo boot component" );            
  243.                 return;            
  244.             }
  245.     
  246.             if( boot.getBootError() != null )
  247.             {
  248.                 $debug( "[onload] skiping load sequence because boot has faulted:" + boot.getBootError() );            
  249.                 lastErrorMsg = boot.getBootError();
  250.                 return;
  251.             }
  252.     
  253.             ///D $debug( "[extension][boot] linked to core feedly boot" );
  254.             boot.registerObserver( that );
  255.     
  256.             // ask been if the core has been loaded. If it has, we know that we need to load
  257.             if( boot.isCoreLoaded() )
  258.                 that.onCoreLoaded( );
  259.         }
  260.         catch( e )
  261.         {
  262.             lastErrorMsg = "failed to load extension boot because:" + e.name + " -- " + e.message + " -- " + e.fileName + " -- " + e.lineNumber;
  263.             $debug( "[extension][boot]" + lastErrorMsg );
  264.         }
  265.       };
  266.     
  267.     that.onUnload = function() 
  268.       {
  269.         getBrowser().removeProgressListener( that );
  270.         getBrowser().removeEventListener( "pageshow", handleOnPageShow, false );
  271.  
  272.         if( that.extension != null )
  273.             that.extension.onUnload();    
  274.         that.extension = null;
  275.  
  276.         if( boot != null )
  277.             boot.unregisterObserver( that );
  278.         boot = null;
  279.  
  280.            that.initialized = false;        
  281.        };
  282.       
  283.     return that;
  284. }();
  285.  
  286. function loadFeedlyExtensionBoot()
  287. {
  288.     window.removeEventListener( "load",     loadFeedlyExtensionBoot ,    false    );
  289.     feedlyExBoot.onLoad();
  290.     ///D $debug( "[extension][boot] loaded" );
  291. }
  292.  
  293. function unloadFeedlyExtensionBoot()
  294. {
  295.     window.removeEventListener( "unload",     unloadFeedlyExtensionBoot , false    );
  296.     feedlyExBoot.onUnload();
  297.     feedlyExBoot = null;
  298.     ///D $debug( "[extension][boot] unloaded" );
  299. }
  300.  
  301.  
  302. window.addEventListener( "load",     loadFeedlyExtensionBoot,     false  );
  303. window.addEventListener( "unload",     unloadFeedlyExtensionBoot,  false  );
  304.